package com.lishe.lishe.lishe.module.myorder.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.bean.SpaceItemDecoration;
import com.lishe.lishe.lishe.module.excitation.activity.DianZanDetailsActivity;
import com.lishe.lishe.lishe.module.myorder.adapter.LogisticsAdapter;
import com.lishe.lishe.lishe.module.myorder.bean.LogisticsObject;
import com.lishe.lishe.lishe.module.myorder.presenter.OrderPresenter;
import com.lishe.lishe.lishe.utils.Utils;
import com.lishe.lishe.retrofit.DataResponseListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by yang on 2018/1/31.
 * <p>
 * 物流信息
 */

public class LogisticsActivity extends BaseActivity {

    public static void jump(Activity activity, String payment_id) {
        Intent intent = new Intent(activity, LogisticsActivity.class);
        intent.putExtra("payment_id", payment_id);
        activity.startActivity(intent);
    }

    private SmartRefreshLayout mSmartLayout;
    private RecyclerView mRecyclerView;
    private Button mButtonBack;
    private TextView mTextTitle,
            mTextEmpty;
    private RelativeLayout mRelativeEmpty;

    private String payment_id;
    private LogisticsAdapter adapter;
    private LinkedList<LogisticsObject> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics);
        payment_id = getIntent().getStringExtra("payment_id");

        initView();
        setListener();
        setAdapter();
        setSmartLayout();
    }

    private void initView() {
        mButtonBack = findViewById(R.id.title_left_button);
        mTextTitle = findViewById(R.id.title_text_title);
        mSmartLayout = findViewById(R.id.refreshlayout);
        mRecyclerView = findViewById(R.id.listView);
        mTextEmpty = findViewById(R.id.empty_text);
        mRelativeEmpty = findViewById(R.id.empty);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(LogisticsActivity.this));
        mRecyclerView.addItemDecoration(new SpaceItemDecoration(0, 0, 0, Utils.dip2px(this, 10)));

        mTextTitle.setText("物流信息");
        mTextEmpty.setText("还没有物流信息哦~");
    }

    private void setListener() {
        mButtonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void setAdapter() {
        mList = new LinkedList<>();
        adapter = new LogisticsAdapter(this, mList);
        mRecyclerView.setAdapter(adapter);

    }

    private void setSmartLayout() {
        mSmartLayout.setEnableLoadmore(false);
        mSmartLayout.autoRefresh(0, 1, 0.62f);

        mSmartLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getExpressList();
            }
        });
    }

    private void getExpressList() {
        if(mRelativeEmpty.getVisibility()==View.VISIBLE){
            mRelativeEmpty.setVisibility(View.GONE);
        }

        OrderPresenter.getInstance().getExpressList(payment_id, new DataResponseListener<List<LogisticsObject>>() {
            @Override
            public void onSuccess(List<LogisticsObject> dataResponse) {
                mSmartLayout.finishRefresh(500, true);
                if (dataResponse != null) {
                    mList.clear();
                    mList.addAll(dataResponse);
                    adapter.notifyDataSetChanged();

                    if(dataResponse.size()==0){
                        mRelativeEmpty.setVisibility(View.VISIBLE);
                    }
                }else{
                    mRelativeEmpty.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFail(String failMessage) {
                mSmartLayout.finishRefresh(1000, true);
                toast(failMessage);

                if(mList.size()==0){
                    mRelativeEmpty.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onError(Throwable e) {
                mSmartLayout.finishRefresh(1000, true);
                toast(R.string.network_or_server_exception);

                if(mList.size()==0){
                    mRelativeEmpty.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}
